#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix-jail
# REQUIRE: ix-warden
# BEFORE: jail
# KEYWORD: shutdown

. /etc/rc.freenas

#generate_plugins_jail_config()
#{
#	local IFS="|"
#	local tmpfile="$(mktemp -q /var/tmp/.rcfooXXXXXX)"
#
#	grep -v "jail_" /etc/rc.conf > "${tmpfile}"
#
#	${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} "
#	SELECT
#		jail_path,
#		jail_name,
#		plugins_path
#	FROM
#		services_pluginsjail
#
#	ORDER BY
#		-services_pluginsjail.id
#
#	LIMIT 1
#	" | \
#	while read -r path name plugins
#	do
#		local mntpath_plugins="${path}/${name}/mnt/plugins"
#		local mntpath_pbi="${path}/${name}/usr/pbi"
#
#		# Skip if jail path name does not exist
#		if [ ! -d "${path}/${name}" ]; then
#			continue
#		fi
#
#		if [ ! -d "${plugins}/pbi" ]
#		then
#			mkdir -p "${plugins}/pbi"
#		fi
#		if [ ! -d "${mntpath_plugins}" ]
#		then
#			mkdir -p "${mntpath_plugins}"
#		fi
#		if [ ! -d "${mntpath_pbi}" ]
#		then
#			mkdir -p "${mntpath_pbi}"
#		fi
#
#		echo "${plugins}	${mntpath_plugins}	nullfs	rw	0	0" | sed 's/ /\\040/g' > "/etc/fstab.${name}"
#		echo "${plugins}/pbi	${mntpath_pbi}	nullfs	rw	0	0" | sed 's/ /\\040/g' >> "/etc/fstab.${name}"
#
#		${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} "
#		SELECT
#			source,
#			destination
#		FROM
#			plugins_nullmountpoint
#		" | \
#		while read -r source destination
#		do
#			echo "${source}	${path}/${name}/${destination}	nullfs	rw	0	0" | sed 's/ /\\040/g' >> "/etc/fstab.${name}"
#		done
#
#		cp /etc/resolv.conf ${path}/${name}/etc/resolv.conf 2> /dev/null
#		cp /etc/localtime ${path}/${name}/etc/localtime 2> /dev/null
#		sysctl security.jail.allow_raw_sockets=1
#
#		# No VIM colors needed here
#		cat<<-__EOF__>>"${tmpfile}"
#
#		jail_${name}_rootdir="${path}/${name}"
#		jail_${name}_hostname="${name}"
#		jail_${name}_devfs_enable="YES"
#		jail_${name}_devfs_ruleset="devfsrules_jail"
#		jail_${name}_procfs_enable="YES"
#		jail_${name}_mount_enable="YES"
#		jail_${name}_vnet_enable="YES"
#		jail_${name}_flags="-l -U root -c persist"
#		jail_${name}_exec_afterstart0="ifconfig lo0 127.0.0.1"
#		jail_${name}_exec_start="/bin/sh /dev/null"
#		jail_sysvipc_allow="YES"
#		jail_list="${name}"
#		jail_v2_enable="YES"
#		jail_enable="YES"
#__EOF__
#	done
#
#	mv "${tmpfile}" /etc/rc.conf
#	return $?
#}


jails_start()
{

	local saved_ifs="${IFS}"
	local module
	local jails="$*"
	local res=0
	RO_FREENAS_CONFIG=$(ro_sqlite ${name} 2> /tmp/${name}.fail && rm /tmp/${name}.fail)
	trap 'rm -f ${RO_FREENAS_CONFIG}' EXIT

	local IFS="|"
	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} "
	SELECT
		module
	FROM
		plugins_kmod

	ORDER BY
		plugin_id,'order'
	" | \
	while read -r module
	do
		/sbin/kldload ${module} > /dev/null 2>&1
	done

	IFS="${saved_ifs}"
	if [ -z "${jails}" ]
	then
		jails=$(jail_get|awk '{ print $1 }')
		for jail in ${jails}
		do
			local auto=$(jail_get_autostart "${jail}")
			local status=$(jail_get_status "${jail}")

			if [ "${auto}" = "Enabled" -a "${status}" = "Stopped" ]
			then
				jail_start ${jail}
				if [ "$?" != "0" ]
				then
					warn "Could not start ${jail}"
					res=1
				fi
			fi
		done
	else
		for jail in ${jails}
		do
			jail_start ${jail}
			if [ "$?" != "0" ]
			then
				warn "Could not start ${jail}"
				res=1
			fi
		done
	fi

	return ${res}
}

jails_stop()
{
	local jails="$*"
	local res=0

	if [ -z "${jails}" ]
	then
		jails=$(jail_get|awk '{ print $1 }')
		for jail in ${jails}
		do
			local status=$(jail_get_status "${jail}")
			if [ "${status}" = "Running" ]
			then
				jail_stop ${jail}
				if [ "$?" != "0" ]
				then
					warn "Could not stop ${jail}"
					res=1
				fi
			fi
		done
	else
		for jail in ${jails}
		do
			jail_stop ${jail}
			if [ "$?" != "0" ]
			then
				warn "Could not stop ${jail}"
				res=1
			fi
		done
	fi

	return ${res}
}

#plugins_jail_stop()
#{
#	if srv_enabled plugins || [ "${rc_force}" = "yes" ]
#	then
#		local jail_name="$(get_plugins_jail_name)"
#		if [ -n "${jail_name}" ]
#		then
#			local tmpfile="$(mktemp -q /var/tmp/.rcfooXXXXXX)"
#
#			/etc/rc.d/jail stop "${jail_name}"
#			grep -v "jail_" /etc/rc.conf > "${tmpfile}"
#			mv "${tmpfile}" /etc/rc.conf
#
#			rm -f "/etc/fstab.${jail_name}"
#		fi
#	fi
#}



name="ix-jail"
start_cmd='jails_start'
stop_cmd='jails_stop'

load_rc_config $name
run_rc_command $*
